﻿/// <reference path="jquery-1.4.4-vsdoc.js"/>
/// <reference path="~/Scripts/Utils.js" />
/// <reference path="~/Scripts/jquery.multiselect.filter.js" />
/*
COPYRIGHT (C) 2011-2012 Raynald Messié

THIS FILE IS PART OF SCRUMPILOT.

SCRUMPILOT IS FREE SOFTWARE: YOU CAN REDISTRIBUTE IT AND/OR MODIFY IT UNDER THE TERMS OF 
THE GNU LESSER GENERAL PUBLIC LICENSE VERSION v2.1 AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION.

SCRUMPILOT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT WITHOUT ANY WARRANTY; WITHOUT
EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE GNU LESSER
GENERAL PUBLIC LICENSE FOR MORE DETAILS.

YOU SHOULD HAVE RECEIVED A COPY OF THE GNU LESSER GENERAL PUBLIC LICENSE ALONG WITH SCRUMPILOT. 
IF NOT, SEE <HTTP://WWW.GNU.ORG/LICENSES/>.
*/

var g_UrlFilters;
var g_UrlContent;
var g_UrlEdit;
var g_UrlData;
var g_UrlLoading;
var g_UrlTaskAction;
var g_UrlSetScore;
var g_UrlMoveItem;

function initTasksList(url, urlContent, urlEdit, urlLoading, urlFilters, urlGetFilters, urlTAction, urlSScore, urlMov) {
    g_UrlFilters = urlFilters;
    g_UrlContent = urlContent;
    g_UrlEdit = urlEdit;
    g_UrlData = url;
    g_UrlLoading = urlLoading;
    g_UrlTaskAction = urlTAction;
    g_UrlSetScore = urlSScore;
    g_UrlMoveItem = urlMov;
    // init checkBoxes
    CallService("GET", urlGetFilters, null, true, function (msg) {
        var fields = new Array("todochk", "progresschk", "donechk", "reopenchk", "unestimatedchk", "taskflt", "bugflt");
        for (var i = 0; i < fields.length; i++) {
            $('#' + fields[i]).attr('checked', msg[i]);
        }
    });
    // init select
    $("#projectsFilter").multiselect({
        noneSelectedText: 'selection',
        minWidth: 50,
        classes: 'projectFilter',
        hide: ["highlight", 800],
        close: function () {
            CallService("POST", "FilterByProject", { myprojects: $('select#projectsFilter').val() }, true, function () {
                return refreshTaskList();
            });
        }
    }).multiselectfilter();
    return refreshTaskList();
}

function refreshTaskList() {
    return loadTaskList(g_UrlContent, g_UrlData, g_UrlEdit, g_UrlLoading);
}

function loadTaskList(urlContent, url, urlEdit, urlLoading) {
    $.get(urlContent, {}, function (html) {
        $.template("WallTemplate", html); // Read template
        BlockUi();
        CallService('GET', url, {}, true, function (data) {
            $('#MainTasksList').empty();
            $.tmpl("WallTemplate", data).appendTo('#MainTasksList'); // apply it
            $('button.buttonClose').button({
                icons: {
                    primary: "ui-icon-close"
                },
                text: false
            });
            $('button.setbutton').button({
                icons: {
                    primary: "ui-icon-circle-check"
                },
                text: false
            });
            $('.edit').editable(urlEdit, {
                indicator: '<img src="' + urlLoading + '">',
                tooltip: "Doubleclick to edit...",
                event: "dblclick",
                callback: function () {
                }
            });
            $('.editarea').editable(urlEdit, {
                type: "autogrow",
                submit: "Save",
                indicator: '<img src="' + urlLoading + '">',
                tooltip: "Doubleclick to edit descriptions...",
                event: "dblclick",
                autogrow: {
                    lineHeight: 16,
                    maxHeight: 512
                },
                callback: function () {

                }
            });
            $('abbr.timeago').timeago();
            $.unblockUI();
        });
    });
}

function FilterView(id) {
    CallService("POST", g_UrlFilters, { key: id, data: isChecked("#" + id) }, true, function () {
        return refreshTaskList();
    });
}

function TaskAction(id, typeAction) {
    CallService("POST", g_UrlTaskAction, { id: id, typeAction: typeAction }, true, function () {
        return refreshTaskList();
    });
}


function EditDialogComplete() {
    refreshTaskList();
    $.unblockUI();
    $('#dialog-score').dialog('close');
}

function showMove(id) {
    BlockUi();
    $("#dialog-move").empty();
    return $.ajax({
        url: g_UrlMoveItem,
        data: { id: id },
        success: function (htmldata) {
            $('#dialog-move').html(htmldata);
            $.unblockUI();
            $('#dialog-move').dialog(
                { autoOpen: false,
                    width: 500,
                    modal: true,
                    hide: "highlight",
                    buttons: {
                        Cancel: function () { $(this).dialog("close"); },
                        "Save": function () { $("#MoveForm").submit(); }
                    }
                }).dialog('open');
        }
    });
}

function initProjectsList() {
    CallService("POST", "FilterByProject", { myprojects: $('select#projectsFilter').val() }, true, function () {
        return refreshTaskList();
    });
}